import cryptoJs from 'crypto-js'
// 加密
export const encryptDes = (message, key) => {
  const keyHex = cryptoJs.enc.Utf8.parse(key)
  const option = { mode: cryptoJs.mode.ECB, padding: cryptoJs.pad.Pkcs7 }
  const encrypted = cryptoJs.DES.encrypt(message, keyHex, option)
  return encrypted.ciphertext.toString()
}
// 解密
export const decryptDes = (message, key) => {
  const keyHex = cryptoJs.enc.Utf8.parse(key)
  const decrypted = cryptoJs.DES.decrypt(
    {
      ciphertext: cryptoJs.enc.Hex.parse(message)
    },
    keyHex,
    {
      mode: cryptoJs.mode.ECB,
      padding: cryptoJs.pad.Pkcs7
    }
  )
  return decrypted.toString(cryptoJs.enc.Utf8)
}
const phoneRule =
  /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[189]))\d{8}$/

const regEmail = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/

/** 手机号校验*/
export const checkPhone = (params) => {
  const regex = new RegExp(phoneRule)
  return regex.test(params)
}
/** 邮箱校验*/
export const checkEmail = (params) => {
  const regex = new RegExp(regEmail)
  return regex.test(params)
}

/** 根据时间戳计算年龄*/
export const getAge = (birthday) => {
  // 出生时间 毫秒
  const birthDayTime = new Date(birthday).getTime()
  // 当前时间 毫秒
  const nowTime = new Date().getTime()
  // 一年毫秒数(365 * 86400000 = 31536000000)
  return Math.floor((nowTime - birthDayTime) / 31536000000)
}

const formatNumber = (n) => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

/** 转换时间格式 xxxx-xx-xx*/
export const formatTime = (date) => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  return `${[year, month, day].map(formatNumber).join('-')}`
}

export function timestampToTime(date) {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const h = date.getHours()
  const m = date.getMinutes()

  return (
    `${[year, month, day].map(formatNumber).join('-')} ` +
    `${[h, m].map(formatNumber).join(':')} `
  )
}

/* 函数节流*/
export function throttle(fn, interval) {
  var enterTime = 0 // 触发的时间
  var gapTime = interval || 300 // 间隔时间，如果interval不传，则默认300ms
  return function() {
    var context = this
    var backTime = new Date() // 第一次函数return即触发的时间
    if (backTime - enterTime > gapTime) {
      fn.call(context, arguments)
      enterTime = backTime // 赋值给第一次触发的时间，这样就保存了第二次触发的时间
    }
  }
}

/* 函数防抖*/
export function debounce(fn, interval) {
  var timer
  var gapTime = interval || 200 // 间隔时间，如果interval不传，则默认1000ms
  return function() {
    clearTimeout(timer)
    var context = this
    var args = arguments // 保存此处的arguments，因为setTimeout是全局的，arguments不是防抖函数需要的。
    timer = setTimeout(function() {
      fn.call(context, args)
    }, gapTime)
  }
}

// 复制文本
export function copyText(text) {
  var textValue = document.createElement('textarea')
  textValue.setAttribute('readonly', 'readonly')
  textValue.value = text
  document.body.appendChild(textValue) // 将textarea添加为body子元素
  textValue.select()
  var res = document.execCommand('copy')
  document.body.removeChild(textValue)// 移除DOM元素
  return res
}
